package com.peng.handler;

import com.peng.common.R;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLIntegrityConstraintViolationException;

@ControllerAdvice
public class AllExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    @ResponseBody
    public R<String> exceptionHandler(SQLIntegrityConstraintViolationException e){
        e.printStackTrace();
        return R.error("用户"+e.getMessage().split(" ")[2]+"已存在");
    }


    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public R<String> exceptionHandler(RuntimeException e){
        e.printStackTrace();
        return R.error(e.getMessage());
    }

}
